package cn.gov.pbc.gd.credit.provider.api.user.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liu.jbase.PageListVo;
import com.liu.jbase.service.db.impl.DBBaseServiceImpl;
import com.liu.tools.PrimaryUtil;

import cn.gov.pbc.gd.credit.api.user.IUserApi;
import cn.gov.pbc.gd.credit.common.constant.CommonConstant;
import cn.gov.pbc.gd.credit.provider.dao.notice.INoticeDao;
import cn.gov.pbc.gd.credit.provider.dao.user.IUserDao;
import cn.gov.pbc.gd.credit.provider.dao.userrole.IUserRoleDao;
import cn.gov.pbc.gd.credit.vo.notice.NoticeVo;
import cn.gov.pbc.gd.credit.vo.user.ResUserVo;
import cn.gov.pbc.gd.credit.vo.user.UserVo;
import cn.gov.pbc.gd.credit.vo.userrole.UserRoleVo;


@Service
public class UserApiImpl extends DBBaseServiceImpl<ResUserVo, UserVo, IUserDao> implements IUserApi{

	
	@Autowired
	private IUserDao userDao;
	
	@Autowired
	private IUserRoleDao userRoleDao;
	
	@Autowired
	private INoticeDao noticeDao;
	
	@Override
	public int insert(UserVo record) {
		
		int t = userDao.insert(record);
		
		if(t == 1){
			List<String> list = record.getRoleIdList();
			UserRoleVo userRoleVo = null;
			if(list != null && list.size() > 0){
				for (int i = 0; i < list.size(); i++) {
					userRoleVo = new UserRoleVo();
					userRoleVo.setId(PrimaryUtil.primaryId("R"));
					userRoleVo.setRoleId(list.get(i));
					userRoleVo.setUserType(1);
					userRoleVo.setCorpUserId(record.getId());
					userRoleDao.insert(userRoleVo);
				}
			}
		} 
				
		return t;
	}
	
	@Override
	public int update(UserVo record) {

		int t = userDao.update(record);
		if(record.getIsModifyRole() != null && record.getIsModifyRole() == 0){
			return t;
		}
		if(t == 1){
			
			UserRoleVo urm = new UserRoleVo();
			urm.setCorpUserId(record.getId());
			userRoleDao.delete(urm);
			
			List<String> list = record.getRoleIdList();			
			if(list != null && list.size() > 0){
							
					UserRoleVo userRoleVo = null;
					
					for (int i = 0; i < list.size(); i++) {
						
						userRoleVo = new UserRoleVo();
						userRoleVo.setId(PrimaryUtil.primaryId("R"));
						userRoleVo.setRoleId(list.get(i));
						userRoleVo.setUserType(1);
						userRoleVo.setCorpUserId(record.getId());
						userRoleDao.insert(userRoleVo);
					}
				
		    }
		}
	
				
		return t;
	}
	
	@Override
	public int delete(UserVo userVo) {
		
		return userDao.delete(userVo);
	}

	@Override
	public int updateStatus(UserVo userVo) {
		
		String oldStatus = "";
		
		switch (userVo.getOldStatus()) {
		case 0:
			oldStatus="待审批";
			break;
		case 1:
			oldStatus="正常";
			break;
		case 2:
			oldStatus="审核失败";
			break;
		case 3:
			oldStatus="锁定";
			break;
		case 4:
			oldStatus="停用";
			break;
		case 5:
			oldStatus="解锁待审批";
			break;
		case 6:
			oldStatus="启用待审批";
			break;
		}
		
		String newStatus= "";
		int noticeType = 1;
		int noticeFlag = 1;
		switch (userVo.getStatus()) {
		case 0:
			newStatus="待审批";
			noticeType = CommonConstant.NOTICE_TYPE_ADD_USER;
			break;
		case 1:
			newStatus="正常";
			noticeType = CommonConstant.NOTICE_TYPE_APPLY_OK;
			noticeFlag = 0;
			break;
		case 2:
			oldStatus="审核失败";
			noticeType = CommonConstant.NOTICE_TYPE_APPLY_FAIL;
			noticeFlag = 0;
			break;
		case 3:
			newStatus="锁定";
			noticeType = CommonConstant.NOTICE_TYPE_LOCK;
			noticeFlag = 0;
			break;
		case 4:
			newStatus="停用";
			noticeType = CommonConstant.NOTICE_TYPE_STOP;
			noticeFlag = 0;
			break;
		case 5:
			newStatus="解锁待审批";
			noticeType = CommonConstant.NOTICE_TYPE_UNLOCK;
			break;
		case 6:
			newStatus="恢复待审批";
			noticeType = CommonConstant.NOTICE_TYPE_UP;
			break;
		}
		int flag = userDao.update(userVo);
		if(flag > 0 && userVo.getStatus() != 1){//非删除用户
			//更新用户成功后在notice表插入更新数据等待审核
			NoticeVo noticeVo=new NoticeVo();
			noticeVo.setId(PrimaryUtil.primaryId("R"));
			noticeVo.setSourceId(userVo.getId());
			noticeVo.setNoticeType(noticeType);
			
			noticeVo.setNoticeFlag(noticeFlag);
			if(noticeFlag == 0){
				noticeVo.setApproveStatus(1);
				noticeVo.setStatus(0);
			} else {
				noticeVo.setStatus(1);
				noticeVo.setApproveStatus(0);
			}
			
			noticeVo.setCorpId(userVo.getCorpId());
			noticeVo.setContent("用户" + userVo.getAccount() + "从-"+oldStatus+"-改为-"+newStatus);
			noticeVo.setUpdateBy(userVo.getUserId());
			noticeVo.setUpdateTime(new Date());
			noticeVo.setCreateBy(userVo.getUserId());
			noticeVo.setCreateTime(new Date());
			noticeDao.insert(noticeVo);
		}
		
		return flag;
	}

	@Override
	public int updateApprove(UserVo vo) {
		
		return userDao.update(vo);
		
	}

	@Override
	public PageListVo<ResUserVo> privilegePage(UserVo entity, PageListVo<ResUserVo> pageListVo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ResUserVo> queryOperaterList(UserVo vo) {
		
		return userDao.queryOperaterList(vo);
	}
	
	
}
